<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\User;

class ConvoyController extends BaseController
{
    /**
     * 车队信息接口
     * by 吉星
     */
    // 车队信息数据
    public function convoy(Request $request){
        if($request){
            $where = [];
            $whereper['branch_name'] = $request['branch_name'] ? $request['branch_name'] : '';
            $whereper['area'] = $request['area'] ? $request['area'] : '';
            $whereper['type'] = $request['type'] ? $request['type'] : '';
            $whereper['starttime'] = $request['starttime'] ? $request['starttime'] : '';
            $whereper['endtime'] = $request['endtime'] ? $request['endtime'] : '';
            if($request['branch_name']){
                $where['b.company_name'] = $request['branch_name'] ? $request['branch_name'] : '';
            }
            if($request['area']){
                $where['b.area'] = $request['area'] ? $request['area'] : '';
            }
            if ($request['type']) {
                $where['b.type'] = $request['type'] ? $request['type'] : '';
            }
            if ($request['starttime'] || $request['endtime']) {
                $starttime = strtotime($request['starttime']);
                $endtime = strtotime($request['endtime']);
                $data = Db::table('convoy as a')
                    ->join('user as u','u.id','=','a.save_userid','left')
                    ->join('branch as b','b.id','=','a.branch_id','left')
                    ->join('driver as d','d.id','=','a.captain_id','left')
                    ->where($where)
                    ->where('a.is_delete','=','1')
                    ->whereBetween('a.save_time',[$starttime,$endtime])
                    ->select('a.*','d.driver_name','u.name as username','a.id as aid','b.area')  //输出车队ID为aid
                    ->get()
                    ->toArray();
            }else{
                $data = Db::table('convoy as a')
                    ->join('user as u','u.id','=','a.save_userid','left')
                    ->join('branch as b','b.id','=','a.branch_id','left')
                    ->join('driver as d','d.id','=','a.captain_id','left')
                    ->where($where)
                    ->where('a.is_delete','=','1')
                    ->select('a.*','d.driver_name','u.name as username','a.id as aid','b.area','b.type')  //输出车队ID为aid
                    ->get()
                    ->toArray();
            }
            $data = json_decode(json_encode($data),true);
            foreach ($data as $key => $value) {
                if($value['area']){
                    $area = Db::table("region")
                        ->where('code',$value['area'])
                        ->first();
                    $area =  json_decode(json_encode($area),true);
                    $paranarea = Db::table('region')
                        ->where('code',$area['parentCode'])
                        ->first();
                    $paranarea =  json_decode(json_encode($paranarea),true);
                    $paranareas = Db::table('region')
                        ->where('code',$paranarea['parentCode'])
                        ->first();
                    $paranareas =  json_decode(json_encode($paranareas),true);
                    $data[$key]['areaname'] = $paranareas['fullName'].$paranarea['fullName'].$area['fullName'];
                }
            }
            if($data){
                $res = ['code'=>1000,'message'=>'获取数据成功!','data'=>$data,'where'=>$whereper];
            }else{
                $res = ['code'=>1002,'message'=>'没有找到符合条件的数据!','where'=>$whereper];
            }
        }else{
            $res = ['code'=>1001,'message'=>'参数错误!'];
        }
        
        return $res;
    }
    // 修改车队信息页面赋值
    public function edit(Request $request){
        if($request['id']){
            $data = Db::table('convoy')->where('id',$request['id'])->first();
            $data = json_decode(json_encode($data),true);
            return ['data'=>$data,'code'=>1000,'message'=>'获取数据成功'];
        }else{
            return ['code'=>1001,'message'=>'请传入车队ID'];
        }
    }
    // 修改车队信息
    public function convoy_edit(Request $request){
        if($request){
            $dat = $request->query();
            $data = array_splice($dat,1);
            dd($data);
            if($data['id']){
                $data['update_user'] = session('uid');
                $data['save_time'] = date('Y-m-d H:i;s',time());
                $save = Db::table('convoy')->where('id','=',$data['id'])->update($data);
                if($save){
                    $res = ['code'=>1000,'message'=>'修改成功!'];
                }else{
                    $res = ['code'=>1001,'message'=>'修改失败,没有数据发生变化!'];
                }
            }else{
                $res = ['code'=>1002,'message'=>'修改失败,没有找到符合条件的数据!'];
            }
        }else{
            $res = ['code'=>1003,'message'=>'参数错误!'];
        }
        return $res;
    }

    // 删除车队 软删除
    public function del(Request $request){
    	if($request['id']){
    		$data['is_delete'] = 2;
    		$data['save_time'] = date('Y-m-d H:i;s',time());
    		$data['save_userid'] = session('uid');
    		$del = Db::table('convoy')->whereIn('id',$request['id'])->update($data);
    		if($del){
    			$res = ['code'=>1000,'message'=>'删除成功!'];
    		}else{
    			$res = ['code'=>1002,'message'=>'删除失败,请确认该数据是否已经删除!'];
    		}
    	}else{
    		$res = ['code'=>1001,'message'=>'未找到要删除的数据!'];
    	}
    	return $res;
    }

    // 关联司机->司机列表
    public function driverlist(Request $request){
    	if($request){
    		// 获取司机
    		$driver = Db::table('driver')
                ->where('is_delete','=',1)
    			->get()
                ->toArray();
    		$driver = json_decode(json_encode($driver),true);
    		
	    	if($driver){
	    		$res = ['code'=>1000,'message'=>'查询数据成功!','data'=>$driver];
	    	}else{
	    		$res = ['code'=>1001,'message'=>'没有查找到司机信息!'];
	    	}
    	}else{
    		$res = ['code'=>1002,'message'=>'参数错误!'];
    	}
    	return $res;
    }
    // 司机关联车队
    public function bdconvoy(Request $request){
    	if($request['id']){
    		$data['convoy_id'] = $request['id']; //车队 ID
    		$data['save_time'] = date('Y-m-d H:i;s',time());
    		$data['save_userid'] = session('uid');
    			$list = Db::table('driver')
    			->whereIn('id',$request['driver_id'])
    			->update($data);
    			if($list){
                    $convoy_drivernum = Db::table('convoy')->where('id',$request['id'])->increment('driver_count',$list); //司机数量增加 XX个
    				$res = ['code'=>1000,'message'=>'司机关联成功!'];
    			}else{
    				$res = ['code'=>1002,'message'=>'关联失败,请确认该司机是否已经关联到该车队!'];
    			}
    	}else{
    		$res = ['code'=>1001,'message'=>'没有找到符合条件的数据!'];
    	}
        return $res;
    }
    
    // 设置队长
    public function duizhang(Request $request){
        if($request['id']){  // 车队ID
            $captain_id = isset($request['driver_id']) ? $request['driver_id'] : ''; //传入新队长司机ID
            // 查看该车队是否已经有队长了
            $driver_dz = Db::table('driver')
                ->where('convoy_id',$request['id'])
                ->where('role',2)
                ->first();
            $driver_dz = json_decode(json_encode($driver_dz),true);
            if($driver_dz && $driver_dz['id'] != $captain_id){
                $data_bg['role'] =1; //原来的队长角色变更为队员
                $data_bg['save_time'] = date('Y-m-d H:i;s',time());
                $data_bg['save_userid'] = session('uid');
                $driver_bg = Db::table('driver') 
                    ->where('id',$driver_dz['id'])
                    ->update($data_bg);
                if($driver_bg){
                    $data_nbg['role'] =2; //新的队长角色从队员变更为队长
                    $data_nbg['save_time'] = date('Y-m-d H:i;s',time());
                    $data_nbg['save_userid'] = session('uid');
                    $driver_nbg = Db::table('driver')
                        ->where('id',$captain_id)
                        ->update($data_nbg);
                }
            }else{
                $data_nbg['role'] =2; //新的队长角色从队员变更为队长
                $data_nbg['save_time'] = date('Y-m-d H:i;s',time());
                $data_nbg['save_userid'] = session('uid');
                $driver_nbg = Db::table('driver')
                    ->where('id',$captain_id)
                    ->update($data_nbg);
            }
            $data['captain_id'] = $$request['driver_id'];
            $data['save_time'] = date('Y-m-d H:i;s',time());
            $data['save_userid'] = session('uid');
            $save = Db::table('convoy')
                ->where('id',$request['id'])
                ->update($data);
            if($driver_nbg || $save){
                $res = ['code'=>1000,'message'=>'设置队长成功'];
            }else{
                $res = ['code'=>1001,'message'=>'设置队长失败,请确认该司机是否已经为该车队队长'];
            }
        }else{
            $res = ['code'=>1002,'message'=>'请传入车队ID'];
        }
        return $res;
    }
    // 添加车队
    public function add(Request $request){
    	if($request){
    		$dat = $request->query();
            $data = array_splice($dat,1);
    		$convoyadd = Db::table('convoy')->insert($data);
    		if($convoyadd){
    			$res = ['code'=>1000,'message'=>'车队增加成功!'];
    		}else{
    			$res = ['code'=>1001,'message'=>'车队增加失败,请刷新后重试!'];
    		}
    	}else{
    		$res = ['code'=>1002,'message'=>'参数错误!'];
    	}
    	return $res;
    }
    // 车队详情页面
    public function convoyitem(Request $request){
        if($request['id']){
            // 车队详情
            $data = Db::table('convoy as c')
                ->join('branch as b','c.branch_id','=','b.id')
                ->join('user as u','u.id','=','c.save_userid')
                ->join('driver as d','d.convoy_id','=','c.id')
                ->where('c.id',$request['id'])
                ->where('d.role',2)
                ->select('*','u.name as username')
                ->first();
            $data = json_decode(json_encode($data),true);
            if($data){
                $res = ['code'=>1000,'message'=>'获取数据成功','data'=>$data];
            }else{
                $res = ['code'=>1001,'message'=>'没有找到符合条件的数据','data'=>$data];
            }
        }else{
            $res = ['code'=>1002,'message'=>'获取数据失败,请传入车队ID'];
        }
        return $res;
    }
    // 详情页绑定司机列表
    public function itemdriverlist(Request $request){
        if($request['id']){ //车队ID
            // 获取司机
            $driver = Db::table('driver as d')
                ->join('user as u','u.id','=','d.save_userid')
                ->where('d.is_delete','=',1)
                ->where('d.convoy_id','=',$request['id'])
                ->select('d.*','u.name')
                ->get()
                ->toArray();
            $driver = json_decode(json_encode($driver),true);
            foreach ($driver as $k => $v) {
                if(in_array($v['attendant'],[1,2,3])){
                    $driver[$key]['is_zaixian'] = "上线";
                }elseif($v['attendant'] == 4){
                    $driver[$key]['is_zaixian'] = "下线";
                }elseif($v['attendant'] == 5){
                    $driver[$key]['renshi'] = "休假中";
                }elseif($v['attendant'] == 6){
                    $driver[$key]['renshi'] = "离职";
                }elseif($v['attendant'] == 1){
                    $driver[$key]['yunying'] = "载客";
                }elseif($v['attendant'] == 2){
                    $driver[$key]['yunying'] = "接单";
                }elseif($v['attendant'] == 3){
                    $driver[$key]['yunying'] = "空驶";
                }elseif($v['attendant'] == 4){
                    $driver[$key]['yunying'] = "停运";
                }
                switch ($v['account_status']) {
                    case '1':
                        $driver[$key]['account_status'] = "正常";
                        break;
                    case '2':
                        $driver[$key]['account_status'] = "未审核";
                        break;
                    case '3':
                        $driver[$key]['account_status'] = "冻结";
                        break;
                    case '4':
                        $driver[$key]['account_status'] = "待激活";
                        break;
                    case '5':
                        $driver[$key]['account_status'] = "审核打回";
                        break;
                    default:
                        # code...
                        break;
                }
            }
            if($driver){
                $res = ['code'=>1000,'message'=>'查询数据成功!','data'=>$driver];
            }else{
                $res = ['code'=>1001,'message'=>'没有查找到司机信息!'];
            }
        }else{
            $res = ['code'=>1002,'message'=>'参数错误!'];
        }
        return $res;
    }
    // 接触绑定的司机
    public function driverjiechu(Request $request){
        // 判断是否传输过来司机ID
        if($request['driver_id']){ //司机ID
            // $request['id']  车队ID
            $data['convoy_id'] = '';
            $data['save_userid'] = session('uid');
            $data['save_time'] = date('Y-m-d H:i;s',time());
            $save = Db::table('driver')
                ->where('id',$request['driver_id'])
                ->update($data);
            if($save){
                $convoy_drivernum = Db::table('convoy')->where('id',$request['id'])->decrement('driver_count'); //司机数量减少一个
                $res = ['code'=>1000,'message'=>'司机解绑成功'];
            }else{
                $res = ['code'=>1001,'message'=>'司机解绑失败,请确认该司机是否已经关联到该车队'];
            }
        }else{
            $res = ['code'=>1002,'message'=>'获取数据失败,请传入司机ID'];
        }
        return $res;
    }
}
